package net.maku.controller;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.exception.HttpException;
import com.wechat.pay.java.core.exception.MalformedMessageException;
import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import com.wechat.pay.java.service.payments.model.Transaction;

import lombok.extern.slf4j.Slf4j;


@Slf4j
public class NewQuickStart {
	
	
	public static void main(String[] args) {
	 //@ApiOperation(value = "预支付订单", notes = "预支付订单")
	    //@PostMapping("/create")
	    //public R<PrepayWithRequestPaymentResponse> createOrder(@Validated @RequestBody CreateOrderReq req) {
	        //创建初始化订单
	    /** 商户号 */
		  String appid = "wx6ed1af0b9fdd0456";
		  //APPID：wx1574a5cc73751091
		  //mch_id：1676811007

		
		  /** 商户号 */
		   String merchantId = "1676811007";

		  /** 商户API私钥路径 */
		  //public static String privateKeyPath = "D:\\workspaces\\workspace202409\\duxinai-end\\cert\\apiclient_cert.p12";
		   String privateKeyPath = "D:\\workspaces\\workspace202409\\duxinai-end\\cert\\apiclient_key.pem";

		  
		  /** 商户证书序列号 */
		   String merchantSerialNumber = "3C8BB16E5EDCBDF6C814AC5276F239401AC401D3";

		  /** 商户APIV3密钥 */
		   String apiV3key = "oXi4b7pE7YU5QRXCHC30tx5yfXJCAyyC";
		 
		  
		  
		RSAAutoCertificateConfig config = new RSAAutoCertificateConfig.Builder()
                .merchantId(merchantId)
                //.privateKey(wxPayConfig.getPrivateKey())
                .privateKeyFromPath(privateKeyPath)
                .merchantSerialNumber(merchantSerialNumber)
                .apiV3Key(apiV3key)
                .build();
	         
	        //请求微信支付相关配置
	        JsapiServiceExtension service =
	                new JsapiServiceExtension.Builder()
	                        .config(config)
	                        .signType("RSA") // 不填默认为RSA
	                        .build();
 
	        
	        
	        
	        
	        
	        
	        
	        
	        
	        PrepayWithRequestPaymentResponse response = new PrepayWithRequestPaymentResponse();
	        try {
	            PrepayRequest request = new PrepayRequest();
	            request.setAppid(appid);
	            request.setMchid(merchantId);
	            //request.setSpAppid(appid);
	            // request.setSpMchid(merchantId);
	            request.setDescription("测试商品标题");
	            request.setOutTradeNo("out_trade_no_002");
	            request.setNotifyUrl("https://notify_url");
	            Amount amount = new Amount();
	            amount.setTotal(100);
	            request.setAmount(amount);
	            Payer payer = new Payer();
	            //payer.setSpOpenid("oyiCY7ZI5zONxJ3f-O_eoV-N1naw");
	            payer.setOpenid("oyiCY7ZI5zONxJ3f-O_eoV-N1naw");
	            request.setPayer(payer);
	            //log.info("请求预支付下单，请求参数：{}", JSONObject.toJSONString(request));
	            // 调用预下单接口
	            //response = service.prepayWithRequestPayment(request, appid);
	            response =service.prepayWithRequestPayment(request);
	            System.out.println( response.toString() );
	            //response = service.prepayWithRequestPayment(request, appid);
	            //response = service.prepayWithRequestPayment(request);
	            //log.info("订单【{}】发起预支付成功，返回信息：{}", order.getOrderNo(), response);
	        } catch (HttpException e) { // 发送HTTP请求失败
	            //log.error("微信下单发送HTTP请求失败，错误信息：{}", e.getHttpRequest());
	            //return R.error().message("下单失败");
	        	e.printStackTrace();
	        } catch (ServiceException e) { // 服务返回状态小于200或大于等于300，例如500
	            //log.error("微信下单服务状态错误，错误信息：{}", e.getErrorMessage());
	            //return R.error().message("下单失败");
	        	e.printStackTrace();
	        } catch (MalformedMessageException e) { // 服务返回成功，返回体类型不合法，或者解析返回体失败
	            //log.error("服务返回成功，返回体类型不合法，或者解析返回体失败，错误信息：{}", e.getMessage());
	            //return R.error().message("下单失败");
	        	e.printStackTrace();
	        }
	      
	        //更新订单状态
	        //return R.ok().data(response);
	    }
}
